package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.MobileSetMealService;
import com.itheima.health.util.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/mobile/setmeal")
public class MobileSetMealController {
    @Autowired
    private MobileSetMealService mobileSetMealService;
    @Autowired
    private CheckItemService checkItemService;
    @Autowired
    private CheckGroupService checkGroupService;
    @Autowired
    private QiniuUtils qiniuUtils;

    /**
     * 查询所有套餐
     * @return
     */
    @GetMapping("/getSetmeal")
    public Result findAllSetmeal() {
        List<Setmeal> setmeals = mobileSetMealService.findAllSetmeal();
        return new Result(true, MessageConst.QUERY_SETMEALLIST_SUCCESS, setmeals);
    }

    /**
     * 根据套餐id查询套餐的详情
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id) {
        //根据套餐的id查询套餐
        Setmeal setmeal = mobileSetMealService.findSetmealById(id);
        List<Integer> groupsIds = mobileSetMealService.findGroupsIdBySetmealId(setmeal.getId());
        List<CheckGroup> checkGroupList = new ArrayList<>();
        for (Integer groupsId : groupsIds) {
            CheckGroup checkGroup = checkGroupService.findById(groupsId);
            List<Integer> checkItemIds = mobileSetMealService.findCheckItemIdsByCheckGroupId(groupsId);
            List<CheckItem> checkItemList = new ArrayList<>();
            for (Integer checkItemId : checkItemIds) {
                CheckItem checkItem = checkItemService.findById(checkItemId);

                checkItemList.add(checkItem);
            }
            checkGroup.setCheckItems(checkItemList);
            checkGroupList.add(checkGroup);
        }
        log.info("图片图片图片图片图片图片图片图片：{}",setmeal.getImg());
        setmeal.setCheckGroups(checkGroupList);
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, setmeal);

    }
}
